package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        // 状态设置为默认状态(预约中)
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        // 到店时间为null
        appointment.setActualArrivalTime(null);
        appointment.setCreateTime(new Date()); // 设置创建时间
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        // 只有状态是预约中才能编辑,不是预约中,不能编辑,抛出异常
        Appointment app = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(app.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能进行编辑操作");
        }
        appointmentMapper.updateByPrimaryKey(appointment);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = get(dictId);
            //只有状态为预约中才能删除
            if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
                throw new BusinessException("只有状态为预约中才能删除");
            }
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        // 取消预约操作需要当前状态为预约中
        Appointment app = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(app.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能进行取消操作");
        }
        appointmentMapper.changStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        // 到店,预约状态才能点击到店
        Appointment app = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(app.getStatus())) {
            throw new BusinessException("只有状态为预约中的预约单才能进行到店操作");
        }
        // 设置预约时间
        appointmentMapper.updateArrival(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        //获取预约单对象
        Appointment appointment = this.get(appointmentId);
        //合理性判断: 已到店 和 已消费 可以点击结算按钮
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) ||
                Appointment.STATUS_SETTLE.equals(appointment.getStatus()))) {
            throw new BusinessException("只有处于已到店和已消费状态才能进行结算单处理");
        }
        //根据预约单Id查询对应结算单对象
        Statement statement = statementService.getByAppointmentId(appointmentId);
        // 为空
        if (statement==null) {
            // 没有找到记录,说明此预约单没有生成结算单记录
            statement = new Statement();
            statement.setAppointmentId(appointmentId);//关联预约单Id
            statement.setCarSeries(appointment.getCarSeries());//汽车分类
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//到店时间
            statement.setCreateTime(new Date());//记录创建时间
            statement.setCustomerName(appointment.getCustomerName());//客户名称
            statement.setCustomerPhone(appointment.getCustomerPhone());//客户手机号码
            statement.setInfo(appointment.getInfo());//备注信息
            statement.setServiceType(appointment.getServiceType());//服务类型(保养或维修
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            //保存结算单
            statementService.save(statement);
        } else {
            //说明有这条结算单记录
        }
        return statement;
    }

    @Override
    public void updateStatus(Long appointmentId, Integer status) {
        appointmentMapper.updateStatus(appointmentId,status);
    }
}
